package com.mrx.agent.plugin;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.utility.JavaModule;

import java.security.ProtectionDomain;
import java.util.List;
import java.util.Set;

/**
 * @author Mr.X
 * @since 2024-11-05 13:36
 **/
public abstract class BasePlugin {

    public abstract List<AgentBuilder.Transformer> transformers();

    public void setArgs(Set<String> args, Class<?> dependencyClass) {
    }

    public abstract Set<String> dependencies();

    public boolean support(String className) {
        return dependencies().contains(className);
    }

    protected void log(String msg) {
        System.out.println(getClass().getSimpleName() + ": " + msg);
    }

    protected interface XTransformer extends AgentBuilder.Transformer {
        @Override
        default DynamicType.Builder<?> transform(
                DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader,
                JavaModule javaModule, ProtectionDomain protectionDomain
        ) {
            return transform(builder, typeDescription);
        }

        DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription);

    }

}
